IF OBJECT_ID('tempdb.dbo.#tmp_spaceused') IS NOT NULL
	DROP TABLE #tmp_spaceused
GO
CREATE TABLE #tmp_spaceused (
	record_id INT IDENTITY(1, 1) NOT NULL
	, table_name NVARCHAR(256) NOT NULL
	, table_rows VARCHAR(11) NULL
	, space_reserved VARCHAR(18) NULL
	, space_data VARCHAR(18) NULL
	, space_index VARCHAR(18) NULL
	, space_unused VARCHAR(18) NULL
)

-- ============
/*<code_comment><![CDATA[declare local variables]]></code_comment>*/
DECLARE	@cursor CURSOR
			, @table_name NVARCHAR(128)
			, @record_id INT
-- ============

-- ============
/*<code_comment><![CDATA[begin cursor: for each table]]></code_comment>*/
SET @cursor = CURSOR FOR
	SELECT TABLE_SCHEMA + '.' + TABLE_NAME
	FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_TYPE = 'BASE TABLE'

OPEN @cursor

FETCH NEXT FROM @cursor INTO @table_name

WHILE @@FETCH_STATUS <> -1
BEGIN

	INSERT #tmp_spaceused
	EXECUTE dbo.sp_spaceused @table_name
	
	SET @record_id = SCOPE_IDENTITY()
	
	UPDATE #tmp_spaceused
	SET table_name = @table_name
	WHERE record_id = @record_id

	FETCH NEXT FROM @cursor INTO @table_name
END
/*<code_comment><![CDATA[end cursor: for each table]]></code_comment>*/
-- ============

-- SELECT * FROM #tmp_spaceused

;
WITH spaceused_numeric AS (
	SELECT PARSENAME(table_name, 2) AS table_schema
		, PARSENAME(table_name, 1) AS table_name
		, table_rows
		, CAST(REPLACE(space_reserved, ' KB', '') AS BIGINT) AS space_reserved_kb
		, CAST(REPLACE(space_data, ' KB', '') AS BIGINT) AS space_data_kb
		, CAST(REPLACE(space_index, ' KB', '') AS BIGINT) AS space_index_kb
		, CAST(REPLACE(space_unused, ' KB', '') AS BIGINT) AS space_unused_kb
	FROM #tmp_spaceused
)
SELECT * FROM spaceused_numeric ORDER BY table_name
